package com.tuanzi.loan.web.bean.family;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.family.ClassificationInfo;
import com.tuanzi.loan.business.service.family.ClassificationService;
import com.tuanzi.loan.business.service.family.MemberRightsService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.MemberRightsRequest;
import com.tuanzi.loan.vo.response.family.MemberRightsResponse;
import com.tuanzi.loan.web.configuration.JSFListingSessionBean;
import com.tuanzi.loan.web.lazy.family.MemeberRightsDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.faces.bean.ViewScoped;
import java.util.List;

import static com.tuanzi.loan.business.constants.DictionaryCode.ONLINE_OFFLINE_STATUS;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class MemberRightsListBean extends JSFListingSessionBean<MemberRightsRequest,MemberRightsResponse> {

    private static final long serialVersionUID = 1706903900973556669L;

    @Autowired
    private MemberRightsService memberRightsService;

    @Autowired
    private ClassificationService classificationService;

    public List<ClassificationInfo> onLoadClassifications(){
        List<ClassificationInfo> classificationInfos = classificationService.loadClassifications();
        return classificationInfos;
    }

    public List<ClassificationInfo> classificationInfos;

    @Override
    protected String page() {
        return "/views/family/member/member_rights_list.xhtml";
    }

    @Override
    protected void initLazyDataModel() {
        $ = new MemberRightsRequest();
        lazyDataModel = new MemeberRightsDataModel($, session, memberRightsService);
        classificationInfos = classificationService.loadClassifications();
    }

    public void onReset() {
        initLazyDataModel();
    }

    public String onAdd(){
        return redirect("/views/family/member/member_rights_add.xhtml");
    }

    public String onEdit(String id) {
        return redirect("/views/family/member/member_rights_add.xhtml?id=" + id);
    }

    public void delete(Long id){
        memberRightsService.deleteRights(id);
        addSuccessfullyMessage();
    }

    @Override
    protected List<String> getDictionaryCodes() {
        return Lists.newArrayList(ONLINE_OFFLINE_STATUS);
    }
}
